import { Component, OnInit } from '@angular/core';

declare var AMap: any;
@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.less'],
})
export class MapComponent implements OnInit {

  public info = JSON.parse(localStorage.getItem('info'));

  public geocoder: any;

  constructor() { }
  getMap() {
    // 实例化地图
    const map = new AMap.Map('container', {
      resizeEnable: true,
      zoom: 18,
       center: [123.447798, 41.722477]
    });

    AMap.service('AMap.Geocoder', () => { // 回调函数
      this.geocoder = new AMap.Geocoder({
        city: '全国'// 城市，默认：“全国”
      });
    });

    // 点击事件
    map.on('click', (e) => {
      const lnglatXY = [e.lnglat.getLng(), e.lnglat.getLat()];
      this.geocoder.getAddress(lnglatXY, (status, result) => {
        const addressInfo = result.regeocode.formattedAddress;
        const info = `姓名：${this.info.name}<br>性别：${this.info.sex}<br>年龄：${this.info.age}<br>电话：${this.info.tel}<br>家庭住址：${this.info.address}`;
        new AMap.InfoWindow({
          content: addressInfo === '辽宁省沈阳市浑南区五三街道IT国际' ? info : addressInfo
        }).open(map, e.lnglat);
      });
    });



  }

  ngOnInit() {
    this.getMap();
  }

}
